package com.ruoyi.develop.goodsReplace.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;
import com.ruoyi.develop.factory.factoryMaterialAssist.mapper.FactoryMaterialAssistMapper;
import com.ruoyi.develop.factory.factoryMaterialMain.mapper.FactoryMaterialMainMapper;
import com.ruoyi.develop.factory.factoryProduceSale.mapper.FactoryProduceSaleMapper;
import com.ruoyi.develop.factory.factoryProduceSaleAll.mapper.FactoryProduceSaleAllMapper;
import com.ruoyi.develop.factory.factoryTestMachineryHammer.mapper.FactoryTestMachineryHammerMapper;
import com.ruoyi.develop.factory.factoryTestMachineryLink.mapper.FactoryTestMachineryLinkMapper;
import com.ruoyi.develop.factory.factoryTestMachineryStick.mapper.FactoryTestMachineryStickMapper;
import com.ruoyi.develop.factory.factoryTestSample.mapper.FactoryTestSampleMapper;
import com.ruoyi.develop.factory.factoryTestSize.mapper.FactoryTestSizeMapper;
import com.ruoyi.develop.factory.factoryTestSurface.mapper.FactoryTestSurfaceMapper;
import com.ruoyi.develop.factory.factoryTestZn.mapper.FactoryTestZnMapper;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.mapper.GoodsOrderInMapper;
import com.ruoyi.develop.goodsOrderMaterials.mapper.GoodsOrderMaterialsMapper;
import com.ruoyi.develop.goodsSale.mapper.GoodsSaleMapper;
import com.ruoyi.develop.goodsSaleChuan.mapper.GoodsSaleChuanMapper;
import com.ruoyi.develop.goodsSaleMaterial.mapper.GoodsSaleMaterialMapper;
import com.ruoyi.develop.goodsSaleMaterialDetail.mapper.GoodsSaleMaterialDetailMapper;
import com.ruoyi.develop.goodsSalebuy.mapper.GoodsSalebuyMapper;
import com.ruoyi.develop.goodsSalebuyall.mapper.GoodsSalebuyallMapper;
import com.ruoyi.develop.goodsSaledo.mapper.GoodsSaledoMapper;
import com.ruoyi.develop.goodsSaledoGongxu.mapper.GoodsSaledoGongxuMapper;
import com.ruoyi.develop.goodsSaledoGongxuUser.mapper.GoodsSaledoGongxuUserMapper;
import com.ruoyi.develop.goodsSaledoGongxuUserLirun.mapper.GoodsSaledoGongxuUserLirunMapper;
import com.ruoyi.develop.lnGongyigoods.mapper.LnGongyigoodsMapper;
import com.ruoyi.develop.lvDaohuoDetail.mapper.LvDaohuoDetailMapper;
import com.ruoyi.develop.lvRukumingxi.mapper.LvRukumingxiMapper;
import com.ruoyi.develop.lvYifahuomingxi.mapper.LvYifahuomingxiMapper;
import com.ruoyi.develop.payApplyBuyDetail.mapper.PayApplyBuyDetailMapper;
import com.ruoyi.develop.produceRecordDetail.mapper.ProduceRecordDetailMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.goodsReplace.mapper.GoodsReplaceMapper;
import com.ruoyi.develop.goodsReplace.domain.GoodsReplace;
import com.ruoyi.develop.goodsReplace.service.IGoodsReplaceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库存编码替换Service业务层处理
 *
 * @author lmm
 * @date 2025-07-31
 */
@Service
public class GoodsReplaceServiceImpl implements IGoodsReplaceService {
    @Resource
    private GoodsReplaceMapper goodsReplaceMapper;
    @Resource
    private GoodsOrderInMapper goodsOrderInMapper;
    @Resource
    private GoodsSaleMapper goodsSaleMapper;
    @Resource
    private GoodsSaledoMapper goodsSaledoMapper;
    @Resource
    private GoodsSalebuyallMapper goodsSalebuyallMapper;
    @Resource
    private GoodsSalebuyMapper goodsSalebuyMapper;
    @Resource
    private LvYifahuomingxiMapper yifahuomingxiMapper;
    @Resource
    private ProduceRecordDetailMapper produceRecordDetailMapper;
    @Resource
    private LvRukumingxiMapper lvRukumingxiMapper;
    @Resource
    private FactoryMaterialMainMapper factoryMaterialMainMapper;
    @Resource
    private FactoryMaterialAssistMapper factoryMaterialAssistMapper;
    @Resource
    private FactoryTestZnMapper factoryTestZnMapper;
    @Resource
    private FactoryTestSizeMapper factoryTestSizeMapper;
    @Resource
    private FactoryTestSurfaceMapper factoryTestSurfaceMapper;
    @Resource
    private FactoryTestSampleMapper factoryTestSampleMapper;
    @Resource
    private FactoryTestMachineryStickMapper factoryTestMachineryStickMapper;
    @Resource
    private FactoryTestMachineryLinkMapper factoryTestMachineryLinkMapper;
    @Resource
    private FactoryTestMachineryHammerMapper factoryTestMachineryHammerMapper;
    @Resource
    private FactoryProduceSaleMapper factoryProduceSaleMapper;
    @Resource
    private FactoryProduceSaleAllMapper factoryProduceSaleAllMapper;
    @Resource
    private GoodsSaledoGongxuMapper goodsSaledoGongxuMapper;
    @Resource
    private GoodsSaledoGongxuUserMapper goodsSaledoGongxuUserMapper;
    @Resource
    private GoodsSaledoGongxuUserLirunMapper goodsSaledoGongxuUserLirunMapper;
    @Resource
    private GoodsSaleMaterialMapper goodsSaleMaterialMapper;
    @Resource
    private GoodsSaleMaterialDetailMapper goodsSaleMaterialDetailMapper;
    @Resource
    private LnGongyigoodsMapper lnGongyigoodsMapper;
    @Resource
    private GoodsOrderMaterialsMapper goodsOrderMaterialsMapper;
    @Resource
    private LvDaohuoDetailMapper daohuoDetailMapper;
    @Resource
    private PayApplyBuyDetailMapper payApplyBuyDetailMapper;
    @Resource
    private GoodsSaleChuanMapper goodsSaleChuanMapper;



    /**
     * 查询库存编码替换
     *
     * @param id 库存编码替换主键
     * @return 库存编码替换
     */
    @Override
    public GoodsReplace selectById(String id) {
        return goodsReplaceMapper.selectById(id);
    }

    /**
     * 查询库存编码替换列表
     *
     * @param entity 库存编码替换
     * @return 库存编码替换
     */
    @Override
    public List<GoodsReplace> selectList(GoodsReplace entity) {
        return goodsReplaceMapper.selectList(entity);
    }

    /**
     * 新增库存编码替换
     *
     * @param entity 库存编码替换
     * @return 结果
     */
    @Override
    public int insert(GoodsReplace entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return goodsReplaceMapper.insert(entity);
    }

    /**
     * 修改库存编码替换
     *
     * @param entity 库存编码替换
     * @return 结果
     */
    @Override
    public int update(GoodsReplace entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return goodsReplaceMapper.update(entity);
    }

    /**
     * 批量删除库存编码替换
     *
     * @param ids 需要删除的库存编码替换主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return goodsReplaceMapper.deleteByIds(ids);
    }

    /**
     * 删除库存编码替换信息
     *
     * @param id 库存编码替换主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return goodsReplaceMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void replace(GoodsReplace entity) {

        GoodsOrderIn goodsRetain = goodsOrderInMapper.selectById(entity.getGoodsId());
        GoodsOrderIn replacedGoods = goodsOrderInMapper.selectById(entity.getReplacedGoodsId());

        Map<String, Object> params = new HashMap<>();
        params.put("goodsId", goodsRetain.getId());
        params.put("goodsCode", goodsRetain.getCode());
        params.put("goodsName", goodsRetain.getName());
        params.put("goodsModel", goodsRetain.getModel());
        params.put("goodsUnit", goodsRetain.getUnit());
        params.put("replacedGoodsId", replacedGoods.getId());
        params.put("replacedGoodsCode", replacedGoods.getCode());
        params.put("replacedGoodsName", replacedGoods.getName());
        params.put("replacedGoodsModel", replacedGoods.getModel());
        params.put("replacedGoodsUnit", replacedGoods.getUnit());


        // 替换步骤
        // 1. 替换订单产品表中数据
        goodsSaleMapper.replaceGoodsInfo(params);
        // 2. 替换请购单明细产品
        goodsSaledoMapper.replaceGoodsInfo(params);
        // 3. 替换采购单明细产品
        goodsSalebuyMapper.replaceGoodsInfo(params);
        goodsSalebuyallMapper.replaceGoodsInfo(params);
        // 4. 替换发货单明细产品
        yifahuomingxiMapper.replaceGoodsInfo(params);
        // 5. 生产入库新 产品明细
        produceRecordDetailMapper.replaceGoodsInfo(params);
        // 6. 出入库明细
        lvRukumingxiMapper.replaceGoodsInfo(params);
        // 到货明细
        daohuoDetailMapper.replaceGoodsInfo(params);
        // 付款申请明细
        payApplyBuyDetailMapper.replaceGoodsInfo(params);
        // 串行拆解
        goodsSaleChuanMapper.replaceGoodsInfo(params);

        // 7. 质检数据
        // 1.主材  2.辅材  3.外观 4.锌层 5.尺寸 6.机械试验
        factoryMaterialMainMapper.replaceGoodsInfo(params);
        factoryMaterialAssistMapper.replaceGoodsInfo(params);
        factoryTestZnMapper.replaceGoodsInfo(params);
        factoryTestSizeMapper.replaceGoodsInfo(params);
        factoryTestSurfaceMapper.replaceGoodsInfo(params);
        factoryTestSampleMapper.replaceGoodsInfo(params);
        factoryTestMachineryStickMapper.replaceGoodsInfo(params);
        factoryTestMachineryLinkMapper.replaceGoodsInfo(params);
        factoryTestMachineryHammerMapper.replaceGoodsInfo(params);
        // 8. 生产单产品
        factoryProduceSaleMapper.replaceGoodsInfo(params);
        factoryProduceSaleAllMapper.replaceGoodsInfo(params);
        goodsSaledoGongxuUserMapper.replaceGoodsInfo(params);
        goodsSaledoGongxuUserLirunMapper.replaceGoodsInfo(params);
        // 9. 报工产品
        goodsSaledoGongxuMapper.replaceGoodsInfo(params);
        // 10. 工艺工序
        // 11. 材料拆解
        goodsSaleMaterialMapper.replaceGoodsInfo(params);
        goodsSaleMaterialDetailMapper.replaceGoodsInfo(params);
        // 12. 材料领用
        goodsOrderMaterialsMapper.replaceGoodsInfo(params);
        lnGongyigoodsMapper.replaceGoodsInfo(params);

        // 更新完 后合并库存
        goodsRetain.setCount(goodsRetain.getCount().add(replacedGoods.getCount()));
        goodsRetain.setRemainderCount(goodsRetain.getRemainderCount().add(replacedGoods.getRemainderCount()));
        goodsOrderInMapper.update(goodsRetain);
        replacedGoods.setCount(BigDecimal.ZERO);
        replacedGoods.setRemainderCount(BigDecimal.ZERO);
        replacedGoods.setDelFlag("886");
        goodsOrderInMapper.update(replacedGoods);
    }


}
